from django.db import models
from cointrades.models.Utils import *
 
class Test(models.Model):
    name = models.CharField(max_length=20)

URL = {
    "hb": "https://api.huobi.pro/market",
    "hbhadax": "https://api.hadax.com/market",
    "ok": "https://www.okcoin.cn/api/v1"
}

method = {
    "hb": {
        "kline" : "/history/kline",
        "depth": "/depth",
        "trade": "/trade",
        "ticker": "/detail/merged",
        "DayDetail": "/detail"
    },
    "hbhadax": {
        "kline" : "/history/kline?symbol=%s&period=%s&size=%s",
        "depth": "/depth",
        "trade": "/trade",
        "ticker": "/detail/merged",
        "DayDetail": "/detail"
    },
    "ok": {
        "kline" : "/kline.do?symbol=%s&type=%s&size=%s&since=%s",
        "depth": "/depth.do?symbol=%s&size=%s&merge=%s", #size: value: must be between 1 - 200 ，merge :value 1, 0.1 (merge depth)
        "trade": "/trades.do?symbol=%s&since=%s",
        "ticker": "/ticker.do?symbol=%s",
        "DayDetail": "undefined.do"
    }
}

# 获取KLine
def get_kline(market, symbol, period, size=150):
    """
    :param symbol
    :param period: 可选值：{1min, 5min, 15min, 30min, 60min, 1day, 1mon, 1week, 1year }
    :param size: 可选值： [1,2000]
    :return:
    """
    params = {'symbol': symbol,
              'period': period,
              'size': size}

    url = URL[market] + method[market]["kline"]
    return http_get_request(url, params)


# 获取marketdepth
def get_depth(market, symbol, type):
    """
    :param symbol
    :param type: 可选值：{ percent10, step0, step1, step2, step3, step4, step5 }
    :return:
    """
    params = {'symbol': symbol,
              'type': type}
    
    url = URL[market] + method[market]["depth"]
    return http_get_request(url, params)


# 获取tradedetail
def get_trade(market, symbol):
    """
    :param symbol
    :return:
    """
    params = {'symbol': symbol}

    url = URL[market] + method[market]["trade"]
    return http_get_request(url, params)


# 获取merge ticker
def get_ticker(market, symbol):
    """
    :param symbol: 
    :return:
    """
    params = {'symbol': symbol}

    url = URL[market] + method[market]["ticker"]
    return http_get_request(url, params)


# 获取 Market Detail 24小时成交量数据
def get_detail(market, symbol):
    """
    :param symbol
    :return:
    """
    params = {'symbol': symbol}

    url = URL[market] + URL[market] + method[market]["DayDetail"]
    return http_get_request(url, params)